package com.ct.rbac.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ct.rbac.convert.SysApisConvertDOToModel;
import com.ct.rbac.mapper.SysApisMapper;
import com.ct.rbac.model.SysApisModel;
import com.ct.rbac.orm.SysApisDO;
import com.ct.rbac.repository.ISysApisRespontory;
import com.ct.repository.commons.BaseServiceImpl;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author Auto Generator
 * @since 2023-08-13
 */
@Service
public class SysApisRespontoryImpl extends BaseServiceImpl<SysApisMapper, SysApisDO> implements ISysApisRespontory {

    @Resource
    private SysApisConvertDOToModel sysApisConvertDOToModel;
    @Resource
    private SysApisMapper sysApisMapper;

    /**
     * 分页查询角色
     *
     * @param status
     * @param keyword
     * @return
     */
    @Override
    public List<SysApisModel> findPageMenu(Integer status, String keyword) {
        LambdaQueryWrapper<SysApisDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Objects.nonNull(status), SysApisDO::getStatus, status);
        lambdaQueryWrapper.and(StringUtils.isNoneEmpty(keyword), c -> c.like(SysApisDO::getTitle, keyword).or().like(SysApisDO::getPath, keyword));
        lambdaQueryWrapper.orderByAsc(SysApisDO::getSorted);
        List<SysApisDO> menusDOList = this.list( lambdaQueryWrapper);
        List<SysApisModel> menuList = tranferListBo(menusDOList,SysApisModel.class);
        List<SysApisModel> newMenuList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(menuList)) {
            // 这里就把菜单进行tree化
            for (SysApisModel sysMenusModel : menuList) {
                if (sysMenusModel.getParentId() == 0L) {
                    newMenuList.add(getMenuTree(sysMenusModel, menuList));
                }
            }
        }
        return newMenuList;
    }

    /**
     * 分页查询角色--租户使用
     * @return
     */
    @Override
    public List<SysApisModel> findSystemPageMenu(Long roleId) {
        List<SysApisDO> menusDOList = this.baseMapper.findRoleApis(roleId);
        List<SysApisModel> menuList = tranferListBo(menusDOList,SysApisModel.class);
        List<SysApisModel> newMenuList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(menuList)) {
            // 这里就把菜单进行tree化
            for (SysApisModel sysMenusModel : menuList) {
                if (sysMenusModel.getParentId() == 0L) {
                    newMenuList.add(getMenuTree(sysMenusModel, menuList));
                }
            }
        }
        return newMenuList;
    }

    /**
     * tree的方法
     *
     * @param result
     * @param allMenuList
     * @return
     */
    private SysApisModel getMenuTree(SysApisModel result, List<SysApisModel> allMenuList) {
        for (SysApisModel sysMenusModel : allMenuList) {
            if (Objects.equals(sysMenusModel.getParentId(), result.getId())) {
                if (result.getChildren() == null) {
                    result.setChildren(new ArrayList<>());
                }
                result.getChildren().add(getMenuTree(sysMenusModel, allMenuList));
            }
        }
        return result;
    }


    /**
     * 根据角色ID查询对应权限
     * @param roleId
     * @return
     */
    @Override
    public List<SysApisModel> findRoleApis(Long roleId){
        List<SysApisDO> roleApis = sysApisMapper.findRoleApis(roleId);
        return sysApisConvertDOToModel.convert(roleApis);
    }

    /**
     * 判断code是否被添加
     * @param code
     * @return
     */
    @Override
    public Boolean countByCode(String code,Long id){
        LambdaQueryWrapper<SysApisDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysApisDO::getCode,code);
        lambdaQueryWrapper.ne(Objects.nonNull(id),SysApisDO::getId,id);
        return this.count(lambdaQueryWrapper) > 0L;
    }

    /**
     * 判断path是否被添加
     * @param path
     * @return
     */
    @Override
    public Boolean countByPath(String path,Long id){
        LambdaQueryWrapper<SysApisDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysApisDO::getPath,path);
        lambdaQueryWrapper.ne(Objects.nonNull(id),SysApisDO::getId,id);
        long count = this.count(lambdaQueryWrapper);
        return  count > 0L;
    }

    /**
     * 更新
     * @param code
     * @return
     */
    @Override
    public SysApisDO getSysApisDOByCode(String code){
        LambdaQueryWrapper<SysApisDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysApisDO::getCode,code);
        return this.getOne(lambdaQueryWrapper);
    }
}
